package de.koller.oceanFramework.util;

import java.util.Arrays;

public class Bytnig {

	public static byte[] floatToByte( float v ) {
		return intToByte( Float.floatToRawIntBits( v ) );
	}
	
	public static float byteToFloat( byte[] b ) {
		return Float.intBitsToFloat( byteToInt(b) );
	}
	
	public static byte[] doubleToByte( double v ) {
		return longToByte( Double.doubleToRawLongBits( v ) );
	}
	
	public static double byteToDouble( byte[] b ) {
		return Double.longBitsToDouble( byteToLong(b) );
	}
	
	public static byte[] intToByte( int v ) {
		return new byte[] {
			(byte)((v >> 24) & 0xff),
			(byte)((v >> 16) & 0xff),
			(byte)((v >> 8) & 0xff),
			(byte)((v >> 0) & 0xff)
		};
	}
	
	public static int byteToInt( byte[] b ) {
		return (int)(
			(0xff & b[0]) << 24 |
			(0xff & b[1]) << 16 |
			(0xff & b[2]) << 8 |
			(0xff & b[3]) << 0
		);
	}
	
	public static byte[] longToByte( long v ) {
		return new byte[] {
			(byte)((v >> 56) & 0xff),
			(byte)((v >> 48) & 0xff),
			(byte)((v >> 40) & 0xff),
			(byte)((v >> 32) & 0xff),
			(byte)((v >> 24) & 0xff),
			(byte)((v >> 16) & 0xff),
			(byte)((v >> 8) & 0xff),
			(byte)((v >> 0) & 0xff)
		};
	}
	
	public static long byteToLong( byte[] b ) {
		return (long)(
			(long)(0xff & b[0]) << 56 |
			(long)(0xff & b[1]) << 48 |
			(long)(0xff & b[2]) << 40 |
			(long)(0xff & b[3]) << 32 |
			(long)(0xff & b[4]) << 24 |
			(long)(0xff & b[5]) << 16 |
			(long)(0xff & b[6]) << 8 |
			(long)(0xff & b[7]) << 0
		);
	}
	
	public static void main( String... args ) {
		long f = 3046212798123847411L;
		
		byte[] b = longToByte( f );
		System.out.println( Arrays.toString( b ) );
		
		long ef = byteToLong( b );
		System.out.println( ef );
	}
	
}
